package com.itheima.service;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsHomeRecommendProductMapper;
import com.itheima.pojo.SmsHomeRecommendProduct;
import com.itheima.pojo.SmsHomeRecommendSubject;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.List;

@DubboService
public class SmsHomeRecommendProductImpl implements SmsHomeRecommendProductApi {
    @Autowired
    private SmsHomeRecommendProductMapper smsHomeRecommendProductMapper;

    /**
     * 创建首页推荐
     *
     * @param smsHomeRecommendProduct
     */
    @Override
    public void save(SmsHomeRecommendProduct smsHomeRecommendProduct) {
        smsHomeRecommendProduct.setRecommendStatus(1);
        smsHomeRecommendProduct.setSort(0);
        smsHomeRecommendProductMapper.insert(smsHomeRecommendProduct);

    }

    /**
     * 批量删除
     *
     * @param ids
     */
    @Override
    public void deleteByIds(Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        smsHomeRecommendProductMapper.deleteBatchIds(list);
    }

    /**
     * 分页 模糊查询
     *
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @param productName
     * @return
     */
    @Override
    public IPage<SmsHomeRecommendProduct> list(Integer pageNum, Integer pageSize, Integer recommendStatus, String productName) {
        IPage<SmsHomeRecommendProduct> page=new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(!ObjectUtils.isEmpty(recommendStatus),SmsHomeRecommendProduct::getRecommendStatus,recommendStatus)
                .like(!ObjectUtils.isEmpty(productName),SmsHomeRecommendProduct::getProductName,productName)
                .orderByDesc(SmsHomeRecommendProduct::getSort);
        smsHomeRecommendProductMapper.selectPage(page,wrapper);
        return page;
    }

    /**
     * 批量修改状态
     *
     * @param recommendStatus
     * @param ids
     */
    @Override
    public void updateByIds(Integer recommendStatus, Integer[] ids) {
        SmsHomeRecommendProduct smsHomeRecommendProduct=new SmsHomeRecommendProduct();
        smsHomeRecommendProduct.setRecommendStatus(recommendStatus);
        for (Integer id : ids) {
            smsHomeRecommendProduct.setId(id.longValue());
            smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
        }
    }


    /**
     * 修改状态
     *
     * @param smsHomeRecommendProduct
     */
    @Override
    public void update(SmsHomeRecommendProduct smsHomeRecommendProduct) {
        smsHomeRecommendProductMapper.updateById(smsHomeRecommendProduct);
    }


    /**
     * 根据id进行查询
     *
     * @param id
     * @return
     */
    @Override
    public SmsHomeRecommendProduct findByID(Integer id) {

        SmsHomeRecommendProduct smsHomeRecommendProduct = smsHomeRecommendProductMapper.selectById(id.intValue());
        return smsHomeRecommendProduct;

    }

    @Override
    public SmsHomeRecommendProduct findByPid(Integer productId) {
        LambdaQueryWrapper<SmsHomeRecommendProduct> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(SmsHomeRecommendProduct::getProductId,productId);
        return smsHomeRecommendProductMapper.selectOne(wrapper);
    }
}
